<template>
	<view  :class="type == 3 ? 'popup2' : ''" class="popup">
		<view class="title" v-if="type != 3">请选择支付方式</view>
		<template v-else>
			<view class="title" >支付</view>
			<view style="font-size:24rpx; color: #562466; text-align:left; margin-top:30rpx;">为什么要支付</view>
			<view style="font-size:20rpx; color: #969696; text-align:left; margin-top:30rpx;">
				为过滤非诚意用户，避免微商，酒托，广告等骚扰，打造纯净私密的空间，保证产品持续稳定运营。需支付一定费用，成为会员
			</view>
		</template>
		<view class="alipay" @click="buy(1)" v-if="zhifubao_flag=='1'">
			<image  :src="$c.config.imageIP + 'yuemini/ic_alipay2.png'"></image>
			<text>支付宝支付</text>
			<view class="money">{{money}}</view>
		</view>
		<view class="wechat" @click="buy(2)"  v-if="weixin_flag=='1'">
			<image  :src="$c.config.imageIP + 'yuemini/ic_wechat2.png'"></image>
			<text>微信支付</text>
			<view class="money">{{money}}</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
			 $c: this.$c,
			 zhifubao_flag:0,
			 weixin_flag:0,
			 postParam: {
			 	type: 2,
			 	userId: '',			 
			 	projectId: '',
			 	openid:'',
			 	appId:'wxff095378edaf00da',
			 },	 
			}
		},
		props:{
			type:{
				type: Number,
				default: 1
			},
			money:{
				type: Number,
				default: 0
			},
			projectId:{
				type: Number,
				default: 0
			}
			
		},
		created() {						
			this.zhifubao_flag = uni.getStorageSync('isaliPay');
			this.weixin_flag = uni.getStorageSync('isWxpay');
		},
		
		methods: {
			schemeurl()
			{	
				
				
				let userInfoID = uni.getStorageSync('userInfoID');
				
				
			    // let userInfo = uni.getStorageSync('userInfo');	
			    // console.log("this.userInfo: " + JSON.stringify(userInfo));		
				// let  namebase64 = this.stringToBase64(this.data.name)
				// let enoname =this.base64ToString(namebase64)
				//  console.log(namebase64+'***55*5***'+this.data.name+'*******'+enoname);
				let querystr = `userId=${userInfoID}&price=${this.money}&type=${this.type}&projectId=${this.projectId}`;
				//let querystr = `userId=${userInfo.id}&price=0.01&type=5&projectId=${this.data.id}`;
				// let pathstr = 'pages/m-pages/m-vip/index?name='+this.data.name;
				// console.log(querystr+'***55*5***');
				let param={
					path:'pages/m-pages/m-personnelList/emptypay',
					query:querystr,
					env_version:'release',//正式版为"release"，体验版为"trial"，开发版为"develop"
					expire_type:1,
					expire_interval:30,
				}
				const wxappid =uni.getStorageSync('wxappid');
				const secret=uni.getStorageSync('secret');
				let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+wxappid+'&secret='+secret;
				uni.request({
					url:url,
					method: 'GET',					
					success: (res) => {				
						//console.log(JSON.stringify(res)+'******f****f****f**');
						let token = res.data.access_token;
						
						let url2 ='https://api.weixin.qq.com/wxa/generate_urllink?access_token='+token;
						uni.request({
							url:url2,
							method:'POST',
							data:param,
							success(result) {
								//console.log(JSON.stringify(result)+'******ggg****gg***gg*');
								// plus.runtime.openURL(result.data.url_link, function(err) {
									
								// });
								const webview = plus.webview.create('', 'custom-webview');
								webview.loadURL(result.data.url_link, {
									'Referer': 'http://mall.ptszy.com://',
								});
							}
							
						});
					}
				});
			},
			async buy(type){
				// #ifdef MP-WEIXIN
				  this.smallwxpay();					  
				// #endif
				// #ifdef  APP-PLUS			
				let url = "/api/aliPayVersion3/transactionsH5?type="+this.type+"&projectId="+this.projectId
				if(type == 2){
					let xiaochengpay = uni.getStorageSync('xiaochengpay');
					console.log(xiaochengpay+'****5*55**'+this.projectId);
					if(xiaochengpay==1)
					{
						console.log('进入小程序支付');
					  this.schemeurl(); 
					   return;
					}else{
					url = "/api/wechatVersion3/rechargeH5?type="+this.type+"&projectId="+this.projectId
					}
				}
				let res = await this.$c.post(url)
				if(res.code != 0){
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
				}
				app.globalData.webviewUrl = res.data.payUrl
				// uni.$emit('webview',{url:res.data.payUrl})
				uni.navigateTo({ url: '/pages/webview/webview'});
				console.log(res.data)
				this.$emit("pay_submit",{});
				// #endif
			},
			smallwxpay() {
				//console.log('******3333333333333******');
				let userInfo = uni.getStorageSync('userInfo');
				let wxopenid = uni.getStorageSync('wxopenid');
					let token = uni.getStorageSync('token');
				console.log(userInfo);
				this.postParam.userId = userInfo.id;
				//this.postParam.price = this.data.price;
				//this.postParam.price ='0.01';
				this.postParam.openid = wxopenid;	
				this.postParam.projectId = this.projectId;
				//192.168.2.218:80  api.ptszyxx.com
				console.log(JSON.stringify(this.postParam)+'*******');
				uni.request({
					url: 'https://api.ptszyxx.com/api/JsApiController/recharge',					   
					method: 'POST',
					data: this.postParam,
					header: {
						'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
						'token':token
					},
					success: (res) => {
						console.log('*******88888*****'+JSON.stringify(res));
						let timeStamp = res.data.data.timeStamp;				
						let paySign = res.data.data.paySign;
						let appId = res.data.data.appId;
						let signType = res.data.data.signType;
						let nonceStr = res.data.data.nonceStr;
						
						wx.requestPayment({
										"timeStamp":timeStamp,
										"nonceStr":nonceStr,
										"package": res.data.data.package,
										"signType": signType,
										"paySign": paySign,
										"success": function(res) {
											console.log('*************成功'+JSON.stringify(res));
											uni.setStorageSync('userVip', 1);		
											uni.showToast({
												title: '会员开通成功',
												duration: 3000
											})																																																										 
										},
										"fail": function(res) {
											console.log('*************失败'+JSON.stringify(res));						
										},
										"complete": function(res) {
						                    console.log('********888888*****complete'+JSON.stringify(res));											
										}
									})									
					}
				})							
			},			
		}
	}
</script>

<style scoped lang="scss">
.popup{
	width:70vw;
	height: 460rpx;
	background-color: #fff;
	padding: 40rpx;
	border-radius: 40rpx;
	text-align: center;
	.title{
		font-size: 36rpx;
		margin-bottom:30rpx;
	}
	.alipay{
		margin-top:60rpx;
		background-color: rgb(71, 169, 246);
		border-radius: 30rpx;
		padding:10rpx 50rpx;
		color:#fff;
		height:80rpx;
		text{
			position:absolute;
			left: 180rpx;
			transform: translateY(20rpx);
		}
		image{
			position:absolute;
			left: 60rpx;
		}
		.money{
			transform: translateY(20rpx);
		}
	}
	.wechat{
		margin-top:60rpx;
		background-color: rgb(101, 212, 64);
		border-radius: 30rpx;
		padding:10rpx 50rpx;
		color:#fff;
		height:80rpx;
		text{
			position:absolute;
			left: 180rpx;
			transform: translateY(20rpx);
		}
		image{
			position:absolute;
			left: 60rpx;
		}
		.money{
			transform: translateY(20rpx);
		}
	}
	.money{
		position:absolute; right:60rpx;
		color:#fff;
	}
	image{
		width:80rpx;
		height:80rpx;
	}
}

.popup2{
	width:100vw !important;
	padding-left:5% !important;
	padding-right:5% !important;
	transform: translateY(50rpx);
}
</style>
